package org.lyq.erp.auth.resources.controller;

import java.util.List;
import javax.annotation.Resource;
import org.lyq.erp.auth.resources.pojo.Resources;
import org.lyq.erp.auth.resources.service.ResourcesService;
import org.lyq.erp.config.json.JSONResult;
import org.lyq.erp.utils.string.ObjectEmptyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({ "/resources" })
public class ResourcesController {
	
	private static Logger logger = LoggerFactory.getLogger(ResourcesController.class);

	@Resource
	private ResourcesService resourcesSercvie;

	@RequestMapping({ "/list" })
	public String list(ModelMap map) {
		
		List<Resources> resList = this.resourcesSercvie.selectALlResources();
		map.put("resList", resList);

		return "resources_list";
	}

	@RequestMapping({ "/insertAndUpdate" })
	public String insertAndUpdate(Resources resources, Integer menuUuid, ModelMap map) {
		if (!insertValidation(resources)) {
			resources.popuperrorMessage = "true";
			map.put("dataBean", resources);
			List<Resources> resList = this.resourcesSercvie.selectALlResources();
			map.put("resList", resList);
			return "resources_list";
		}
		try {
			if (resources.getResUuid() == null) {
				this.resourcesSercvie.insert(resources);
			} else {
				this.resourcesSercvie.update(resources);
			}
		} catch (Exception e) {

			logger.info(e.getMessage());
			resources.addErrorMessage("res004");
			resources.popuperrorMessage = "true";
			map.put("dataBean", resources);
			List<Resources> resList = this.resourcesSercvie.selectALlResources();
			map.put("resList", resList);
			return "resources_list";
		}
		return "redirect:/resources/list?menuUuid=" + menuUuid;
	}

	@RequestMapping({ "/getResources" })
	@ResponseBody
	public JSONResult getResources(Integer resUuid) {
		if (resUuid != null) {
			Resources resources = this.resourcesSercvie.selectByPrimaryKey(resUuid);
			if (resources != null) {
				return JSONResult.ok(resources);
			}
			return JSONResult.errorMsg("res003");
		}

		return JSONResult.errorMsg("res003");
	}

	@RequestMapping({ "/delete" })
	public String delete(Resources resources, Integer menuUuid, ModelMap map) {
		if (resources.getResUuid() != null) {
			try {
				this.resourcesSercvie.delete(resources.getResUuid());
				return "redirect:/resources/list?menuUuid=" + menuUuid;
			} catch (Exception e) {
				logger.info(e.getMessage());
			}
		}
		resources.addErrorMessage("res005");
		map.put("dataBean", resources);
		List<Resources> resList = this.resourcesSercvie.selectALlResources();
		map.put("resList", resList);
		return "resources_list";
	}

	private boolean insertValidation(Resources resources) {
		if (ObjectEmptyUtils.isEmptyString(resources.getResName())) {
			resources.addErrorMessage("resName", "res001");
		}
		if (ObjectEmptyUtils.isEmptyString(resources.getResUrl())) {
			resources.addErrorMessage("resUrl", "res002");
		}
		return resources.getMap().isEmpty();
	}
}
